//
// BonusManager.cpp for nibbler in /home/lefebv_w//workspace/cpp/nibbler/googleCode
// 
// Made by damien lefebvre
// Login   <lefebv_w@epitech.net>
// 
// Started on  Thu Mar 21 17:58:19 2013 damien lefebvre
// Last update Sun Mar 24 18:55:17 2013 damien lefebvre
//

#include	"BonusManager.hh"

BonusManager::BonusManager(void)
{
  m_parts.push_back(new BonusPart());
}

BonusManager::~BonusManager(void)
{
  for (unsigned int i = 0; i < m_parts.size(); i++)
    {
      delete m_parts[i];
    }
}

void		BonusManager::initialize(t_frame frame, std::vector< std::vector<Case *> > const & map, Snake const & snake)
{
  for (unsigned int i = 0; i < m_parts.size(); i++)
    {
      m_parts[i]->generatePosition(frame, map, snake);
    }
  m_5part.generatePosition(frame, map, snake);
  //m_life.generatePosition(frame, map, snake);
}

void		BonusManager::move(t_frame frame, std::vector< std::vector<Case *> > const & map, Snake const & snake)
{
  m_5part.move(frame, map, snake);
  for (unsigned int i = 0; i < m_parts.size(); i++)
    {
      m_parts[i]->move(frame, map, snake);
    }
  //m_life.move(frame, map, snake);
}

void		BonusManager::addBonusPart(t_frame frame, std::vector< std::vector<Case *> > const & map, Snake const & snake)
{
  m_parts.push_back(new BonusPart());
  m_parts[m_parts.size() - 1]->generatePosition(frame, map, snake);
}

bool		BonusManager::checkSnakePosition(t_frame frame, Score & score, std::vector< std::vector<Case *> > const & map, Snake & snake)
{
  if (m_5part.checkSnakePosition(frame, snake))
    {
      if (score.eat(5) && score.getLevel() <= 5)
	{
	  addBonusPart(frame, map, snake);
	  // m_life.generatePosition(frame, map, snake);
	}
      if (snake.addPart(map, 5))
	return (true);
      m_5part.generatePosition(frame, map, snake);
    } 
  for (unsigned int i = 0; i < m_parts.size(); i++)
    {
      if (m_parts[i]->checkSnakePosition(frame, snake))
	{
	  if (score.eat(1) && score.getLevel() <= 5)
	    {
	      addBonusPart(frame, map, snake);
	      //  m_life.generatePosition(frame, map, snake);
	    }
	  if (snake.addPart(map))
	    return (true);
	  m_parts[i]->generatePosition(frame, map, snake);
	}
    }
  /*if (m_life.checkSnakePosition(frame, snake))
    {
      score.addLife();
      score.add(10);
      }*/
}

void		BonusManager::draw(t_frame frame, IGraphicLibrary & graph) const
{
  m_5part.draw(frame, graph);
  for (unsigned int i = 0; i < m_parts.size(); i++)
    {
      m_parts[i]->draw(frame, graph);
    }
  //  m_life.draw(frame, graph);
}
